package com.kakarote.hrm.controller;

import com.kakarote.core.common.Result;
import com.kakarote.hrm.constant.EmployeeEncryptFieldTypeEnum;
import com.kakarote.hrm.entity.PO.HrmEmployeeEncryptField;
import com.kakarote.hrm.service.IHrmEmployeeEncryptFieldService;
import com.kakarote.hrm.service.IHrmEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 自定义加密字段 前端控制器
 * </p>
 *
 * @author guomenghao
 * @since 2021-04-14
 */
@RestController
@RequestMapping("/hrmEmployeeEncryptField")
@Api(tags = "员工管理-自定义加密字段")
public class HrmEmployeeEncryptFieldController {

    @Autowired
    private IHrmEmployeeEncryptFieldService employeeEncryptFieldService;
    @Autowired
    private IHrmEmployeeService employeeService;


    @PostMapping("/encryptionGroup/{employeeId}")
    @ApiOperation("判断当前用户是否加密，加密字段分组")
    public Result<Map<String, Object>> queryEncryptionGroup(@PathVariable("employeeId") Long employeeId) {
        HashMap<String, Object> map = new HashMap<>();
        // 判断是否为加密人员
        Boolean isExist = employeeService.deptExistByEmployeeId(82200023L, employeeId);
        //map.put("isEncrypt", isExist && !UserUtil.isAdmin());
        map.put("isEncrypt", isExist);
        map.put("fieldGroup", employeeEncryptFieldService.fieldListGroupByType());
        return Result.ok(map);
    }

    @GetMapping("/fieldGroup")
    @ApiOperation("加密字段类型分组")
    public Result<Map<String, List<HrmEmployeeEncryptField>>> queryFieldGroup() {
        Map<String, List<HrmEmployeeEncryptField>> fieldGroup = new HashMap<>();
        List<HrmEmployeeEncryptField> list = employeeEncryptFieldService.lambdaQuery().list();
        if (list != null && !list.isEmpty()) {
            fieldGroup = list.stream().collect(Collectors.groupingBy(item -> EmployeeEncryptFieldTypeEnum.parseName(item.getType()), Collectors.toList()));
        }
        return Result.ok(fieldGroup);
    }

    @PostMapping("/setFieldList")
    @ApiOperation("修改加密字段集合")
    public Result setFieldList(@RequestBody List<HrmEmployeeEncryptField> fieldList) {
        employeeEncryptFieldService.setFieldList(fieldList);
        return Result.ok();
    }
}

